import { Entity, Column, PrimaryGeneratedColumn, OneToMany } from 'typeorm'
import { OrderItemEntity } from './order-item.entity'
import { RefundEntity } from '@/src/refund/entities/refund.entity'

@Entity('order')
export class OrderEntity {
  @PrimaryGeneratedColumn()
  id: number // 标记为主列，值自动生成

  @Column({ type: 'int', comment: '用餐方式：1堂食 2外带' })
  dining_method: number

  @Column({ type: 'varchar', length: 255, nullable: true, comment: '备注' })
  remark?: string | null

  @Column({ type: 'tinyint', default: 0, comment: '状态：0待支付 1已支付 2已完成 3退款中 5已退款 6退款驳回' })
  status: number

  @Column({ type: 'int', comment: '用户ID' }) // 关联下单用户
  user_id: number

  @Column({ default: Math.floor(Date.now() / 1000) })
  create_time: number

  @Column({ type: 'decimal', precision: 10, scale: 2, default: 0 })
  total_fee: number

  @Column({ type: 'tinyint', default: 0 })
  is_del?: number

  @Column({ type: 'boolean', default: false, comment: '是否评价' })
  reviewed: boolean

  // 关联订单项（一对多）
  @OneToMany(() => OrderItemEntity, (item) => item.order, {
    cascade: true, // 保存订单时自动保存订单项
    eager: true // 查询订单时自动加载订单项
  })
  items: OrderItemEntity[]

  // 关联退款记录
  @OneToMany(() => RefundEntity, (refund) => refund.order, {
    eager: false, // 不自动加载
    nullable: true // 允许为null
  })
  refund: RefundEntity[]
}